import { Router } from 'express';
import { authenticate, authorize } from '../../middleware/auth';
import { RoleController } from '../../admin/controllers/roleController';
import { logOperation } from '../../middleware/operationLog';
import { ALL_PERMISSIONS } from '../../constants/permissions';
import { skipLog } from '../../middleware/operationLog';

const router = Router();
const roleController = new RoleController();

/**
 * @swagger
 * /api/roles:
 *   get:
 *     summary: 获取角色列表
 *     tags: [角色管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 20
 *         description: 每页数量
 *       - in: query
 *         name: search
 *         schema:
 *           type: string
 *         description: 搜索关键词
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [ACTIVE, INACTIVE]
 *         description: 角色状态
 *     responses:
 *       200:
 *         description: 获取成功
 *   post:
 *     summary: 创建角色
 *     tags: [角色管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *             properties:
 *               name:
 *                 type: string
 *                 description: 角色名称
 *               description:
 *                 type: string
 *                 description: 角色描述
 *               status:
 *                 type: string
 *                 enum: [ACTIVE, INACTIVE]
 *                 default: ACTIVE
 *                 description: 角色状态
 *               permissionIds:
 *                 type: array
 *                 items:
 *                   type: integer
 *                 description: 权限ID列表
 *     responses:
 *       201:
 *         description: 创建成功
 */
router.get('/', authenticate, authorize([ALL_PERMISSIONS.ROLE_READ]), skipLog, roleController.getRoles);
router.post('/', 
  authenticate, 
  authorize([ALL_PERMISSIONS.ROLE_CREATE]),
  logOperation('角色创建', '角色管理'),
  roleController.createRole
);

/**
 * @swagger
 * /api/roles/stats:
 *   get:
 *     summary: 获取角色统计信息
 *     tags: [角色管理]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 */
router.get('/stats', authenticate, authorize([ALL_PERMISSIONS.ROLE_READ]), skipLog, roleController.getRoleStats);

/**
 * @swagger
 * /api/roles/{id}:
 *   get:
 *     summary: 根据ID获取角色
 *     tags: [角色管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 角色ID
 *     responses:
 *       200:
 *         description: 获取成功
 *   put:
 *     summary: 更新角色
 *     tags: [角色管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 角色ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 角色名称
 *               description:
 *                 type: string
 *                 description: 角色描述
 *               status:
 *                 type: string
 *                 enum: [ACTIVE, INACTIVE]
 *                 description: 角色状态
 *     responses:
 *       200:
 *         description: 更新成功
 *   delete:
 *     summary: 删除角色
 *     tags: [角色管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 角色ID
 *     responses:
 *       200:
 *         description: 删除成功
 */
router.get('/:id', authenticate, authorize([ALL_PERMISSIONS.ROLE_READ]), skipLog, roleController.getRoleById);
router.put('/:id', 
  authenticate, 
  authorize([ALL_PERMISSIONS.ROLE_UPDATE]),
  logOperation('角色更新', '角色管理'),
  roleController.updateRole
);
router.delete('/:id', 
  authenticate, 
  authorize([ALL_PERMISSIONS.ROLE_DELETE]),
  logOperation('角色删除', '角色管理'),
  roleController.deleteRole
);

/**
 * @swagger
 * /api/roles/{id}/permissions:
 *   get:
 *     summary: 获取角色权限
 *     tags: [角色管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 角色ID
 *     responses:
 *       200:
 *         description: 获取成功
 *   put:
 *     summary: 更新角色权限
 *     tags: [角色管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 角色ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - permissionIds
 *             properties:
 *               permissionIds:
 *                 type: array
 *                 items:
 *                   type: integer
 *                 description: 权限ID列表
 *     responses:
 *       200:
 *         description: 更新成功
 */
router.get('/:id/permissions', authenticate, authorize([ALL_PERMISSIONS.ROLE_READ]), skipLog, roleController.getRolePermissions);
router.put('/:id/permissions', 
  authenticate, 
  authorize([ALL_PERMISSIONS.ROLE_UPDATE]),
  logOperation('角色权限更新', '角色管理'),
  roleController.assignPermissions
);

export default router; 